﻿using DevExpress.XtraReports.UI;
using System;
using System.Data;
using System.Drawing;

namespace ConfExpert.Forms.Rapor.YeniYapi
{
    public partial class KoliUstu7C : DevExpress.XtraReports.UI.XtraReport
    {
        DataSet dsKoliUstu; 
        int EmptyLabelCount;
        public bool Stop = false;
        public KoliUstu7C(DataSet ds)
        {
            InitializeComponent();
            dsKoliUstu = ds;

            tbKoliUstu.Borders = DevExpress.XtraPrinting.BorderSide.All;
            this.DataMember = "Table";
            this.DataSource = dsKoliUstu;
        }

        private DataTable RemoveUnusedColumns(DataTable dt)
        {
            string colNames = "";
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                string activeColName = dt.Columns[i].ColumnName;
                if (activeColName.StartsWith("A") )
                {
                    Int32 sum = Convert.ToInt32(dt.Select("ISNULL([" + activeColName + "],0)<>0").Length);
                    if (sum == 0)
                    {
                        colNames += dt.Columns[i].ColumnName + ";";

                    }
                    //else
                    //    break;
                }
                else
                    colNames += dt.Columns[i].ColumnName + ";";
            }

            //for (int i = dt.Columns.Count - 1; i > 0; i--)
            //{
            //    string activeColName = dt.Columns[i].ColumnName;
            //    if (activeColName.StartsWith("A") )
            //    {
            //        Int32 sum = Convert.ToInt32(dt.Select("ISNULL([" + activeColName + "],0)<>0").Length);
            //        if (sum == 0)
            //        {
            //            colNames += dt.Columns[i].ColumnName + ";";
            //        }
            //        else
            //            break;
            //    }
            //    else
            //        colNames += dt.Columns[i].ColumnName + ";";
            //}

            foreach (string colName in colNames.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
            {
                dt.Columns.Remove(colName);
            }

            return dt;
        }

        private void AddBDColumns(string colName)
        {
            string caption;
            if (colName != "DTotal")
            {
                caption =  colName.Substring(1);
                //DataRow dr = dsKoliUstu.Tables[0].Select("Name='" + colName + "'")[0];
                //caption = dr["Beden"].ToString();
            }

            else
            {
                caption = "TOT";
            }
            XRTableCell cell = new XRTableCell();
            cell.Name = "header" + colName;
            cell.Text = caption;//colName.Substring(1, colName.Length - 1);
            cell.WidthF = 80F;

            XRTableCell detailCell = new XRTableCell();
            detailCell.Name = "detail" + colName;
            detailCell.Font = new System.Drawing.Font("Arial", 11F, FontStyle.Bold);
            detailCell.DataBindings.AddRange(new DevExpress.XtraReports.UI.XRBinding[] {
            new DevExpress.XtraReports.UI.XRBinding("Text", null, "Table."+colName)});
            detailCell.WidthF = 80F;

            //if (colName == "DTotal")
            //{
            //    cell.Text = "TOT";
            //    cell.WidthF = detailCell.WidthF = 25F;

            //    cell.Borders = ((DevExpress.XtraPrinting.BorderSide)((((DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Top)
            //        | DevExpress.XtraPrinting.BorderSide.Right)
            //        | DevExpress.XtraPrinting.BorderSide.Bottom)));

            //    detailCell.Borders =
            //        ((DevExpress.XtraPrinting.BorderSide)(((DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Right)
            //        | DevExpress.XtraPrinting.BorderSide.Bottom)));
            //    detailCell.Font = new System.Drawing.Font("Calibri", 9.75F);
            //}

            trHeader.Cells.Add(cell);
            trDetail.Cells.Add(detailCell);
        }

        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            EmptyLabelCount--;

            this.DataMember = "Table";
            this.DataSource = dsKoliUstu;

            tcArtikel.DataBindings.Add("Text", null, "Table.Artikel");
            tcModel.DataBindings.Add("Text", null, "Table.Model");
            //tcLot.DataBindings.Add("Text", null, "Table.Lot");
            txBoxNr.DataBindings.Add("Text", null, "Table.BoxNr");
            tcTotal.DataBindings.Add("Text", null, "Table.Total");

            //xrTableCell1.WidthF = txBoxNr.WidthF = 95.18F;
            //xrTableCell2.WidthF = tcModel.WidthF = 247.22F;
            //xrTableCell4.WidthF = tcArtikel.WidthF = 220.82F;
            //xrTableCell5.WidthF = tcLot.WidthF = 100.76F;
            
            //int columncount = trHeader.Cells.Count ;
            //for (int i = trHeader.Cells.Count; i >=  5; i--)
            //{
            //    trHeader.Cells[i-1].Dispose();
            //    trDetail.Cells[i-1].Dispose();
            //}

            //tbKoliUstu.WidthF = 300F;
            tbKoliUstu.BeginInit();

            trHeader.Cells.Clear();
            trDetail.Cells.Clear();

            int Boxnr = Convert.ToInt16(GetCurrentColumnValue("BoxNr"));
            
            DataTable dt = new DataTable();
            dt.Merge(dsKoliUstu.Tables[0].Select("BoxNr="+Boxnr).CopyToDataTable());
            DataTable dtBeden = RemoveUnusedColumns(dt);

            for (int i = 0; i < dtBeden.Columns.Count; i++)
            {
                string colName = dtBeden.Columns[i].ColumnName;
                AddBDColumns(colName);
            }
            float wdt = 0;
            if (dtBeden.Columns.Count * 80 < 950)
                wdt = dtBeden.Columns.Count * 80;
            else
                wdt = 950;
            tbKoliUstu.WidthF = wdt;
            tbKoliUstu.Borders = DevExpress.XtraPrinting.BorderSide.All;
            tbKoliUstu.LeftF = 475 - dtBeden.Columns.Count * 40;
            tbKoliUstu.EndInit();
        }

        private void KoliUstu7C_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            //this.DataMember = "Table";
            //this.DataSource = dsKoliUstu;

            //tcArtikel.DataBindings.Add("Text", null, "Table.Artikel");
            //tcModel.DataBindings.Add("Text", null, "Table.Model");
            //tcLot.DataBindings.Add("Text", null, "Table.Lot");
            //txBoxNr.DataBindings.Add("Text", null, "Table.BoxNr");

            //DataTable dt = new DataTable();
            //dt.Merge(dsKoliUstu.Tables[0]);
            //DataTable dtBeden = RemoveUnusedColumns(dt);

            //for (int i = 0; i < dtBeden.Columns.Count; i++)
            //{
            //    string colName = dtBeden.Columns[i].ColumnName;
            //    AddBDColumns(colName);
            //}
            //tbKoliUstu.WidthF = 1000F;
        }

        private void xrTable1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (EmptyLabelCount > -1)
            {
                e.Cancel = true;
            } 
        }

        private void tbKoliUstu_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (EmptyLabelCount > -1)
            {
                e.Cancel = true;
            }  
        }

        private void ReSize(object sender)
        {
            //Resize ı düzgün yapan tek fonksiyon !!!
            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            gr.PageUnit = GraphicsUnit.Millimeter;
            int factor = 10;

            XRTableCell label = (XRTableCell)sender;

            float newSize = 9.75f;
            Font defaultFont = new System.Drawing.Font("Calibri", newSize);
            
            float stringWidth = gr.MeasureString(label.Text, defaultFont).Width;
            float labelWidth = label.WidthF;


            if (labelWidth < stringWidth * factor)
                newSize = defaultFont.Size * label.WidthF / stringWidth / factor;

            if (label.Name == "xrTableCell1")
                label.Font = new System.Drawing.Font(defaultFont.FontFamily, newSize, FontStyle.Bold);
            else
                label.Font = new Font(defaultFont.FontFamily, newSize);
            gr.Dispose();
        }

        private void tcModel_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void tcArtikel_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void xrTableCell1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

    }
}
